package com.nai365.nart.operation.controller;

import com.nai365.nart.common.core.util.R;
import com.nai365.nart.operation.dto.DrawLotsSinUpDto;
import com.nai365.nart.operation.entity.DrawLotsSignUp;
import com.nai365.nart.operation.entity.OptDrawLotsLog;
import com.nai365.nart.operation.service.DrawLotsSignUpService;
import com.nai365.nart.operation.service.OptDrawLotsLogService;
import com.nai365.nart.operation.vo.DrawLotsSignUpVo;
import com.nai365.nart.operation.vo.DrawLotsVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 抽签相关
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/drawLots" )
@Api(value = "drawLots", tags = "抽签相关")
public class DrawLotsController {

	private final DrawLotsSignUpService drawLotsSevice;

	private final OptDrawLotsLogService optDrawLotsLogService;

	/**
	 * 用户报名
	 * @param drawLotsSignUpVo
	 * @return
	 */
	@ApiOperation(value = "用户抽签报名", notes = "用户抽签报名")
	@PostMapping("signUp")
	public R<Boolean> drawLotsSignUp(@RequestBody DrawLotsSignUpVo drawLotsSignUpVo){
		DrawLotsSignUp userSignUp = drawLotsSevice.findUserSignUp(drawLotsSignUpVo.getUserId(), drawLotsSignUpVo.getGoodsId());
		if(!ObjectUtils.isEmpty(userSignUp)){
			return R.ok(Boolean.TRUE,"用户已报名不允许重复报名!");
		}
		DrawLotsSignUp drawLotsSignUp = new DrawLotsSignUp().setGoodsId(drawLotsSignUpVo.getGoodsId()).setUserId(drawLotsSignUpVo.getUserId());
		Boolean isSuccess=drawLotsSevice.saveDrawLotsSignUp(drawLotsSignUp);
		return R.ok(isSuccess,"报名成功！");
	}

	/**
	 * 根据用户id查询报名信息
	 * @param userId
	 * @return
	 */
	@ApiOperation(value = "根据用户id查询报名信息", notes = "根据用户id查询报名信息")
	@GetMapping("findUserSignUpById/{userId}")
	public R<List<DrawLotsSignUp>> findUserSignUpById(@PathVariable("userId") Long userId){
		List<DrawLotsSignUp> drawLotsSignUp=drawLotsSevice.findUserSignUpById(userId);
		return R.ok(drawLotsSignUp);
	}

	/**
	 * 根据用户id,goodsId 查询报名信息
	 * @param userId
	 * @param goodsId
	 * @return DrawLotsSignUp @{DrawLotsSignUp}
	 */
	@ApiOperation(value = "根据用户id,商品id 报名信息", notes = "根据用户id,商品id 报名信息")
	@GetMapping("findUserSignUp/{userId}/{goodsId}")
	public R<DrawLotsSignUp> findUserSignUp(@PathVariable("userId") Long userId,@PathVariable("goodsId") Long goodsId){
		DrawLotsSignUp drawLotsSignUp=drawLotsSevice.findUserSignUp(userId,goodsId);
		return R.ok(drawLotsSignUp);
	}

	/**
	 * 根据商品id 开奖份数 totalNum 抽签创单
	 *
	 *
	 */
	@ApiOperation(value = " 开奖创单", notes = " 开奖创单")
	@PostMapping("drawLots")
	public R<Boolean> drawLots(@RequestBody DrawLotsVo drawLotsVo){
		List<OptDrawLotsLog> drawLogs=optDrawLotsLogService.drawLots(drawLotsVo);
		if(drawLogs.isEmpty()){
			return R.failed(Boolean.FALSE,"创单失败！");
		}else {
			return R.ok(Boolean.TRUE,"创单成功了！");
		}

	}
	/**
	 * @param userId
	 * @param goodsId
	 * 根据用户id 商品good_id 用户的抽签结果
	 */
	@ApiOperation(value = "根据用户id 商品good_id 查询用户的抽签结果", notes = "根据用户id 商品good_id 用户的抽签结果")
	@GetMapping("findUserdrawLots/{userId}/{goodsId}")
	public R<DrawLotsSinUpDto> findUserdrawLotsByUserIdAndGoodsId(@PathVariable("userId") Long userId, @PathVariable("goodsId") Long goodsId){
		DrawLotsSinUpDto oll=optDrawLotsLogService.findUserdrawLotsByUserIdAndGoodsId(userId,goodsId);
		return R.ok(oll);
	}

}
